import QtQuick 2.0
import VPlay 2.0
 // needed for Body.Static
import "../scripts/collisionLogic.js" as CollisionLogic

EntityBase {
  entityType: "platform"
  id: platform
  poolingEnabled: true
  z:1

  Component.onCompleted: console.debug("Platform.onCompleted()")
  Component.onDestruction: console.debug("Platform.onDestruction()")

  property bool colliding;
  signal properCollision;

  Image {
    id: sprite
    source: "../../assets/img/platform.png"

    width: level.gridSize
    height: 8

    anchors.centerIn: parent
  }

  BoxCollider {
    id: collider
    bodyType: Body.Static
    collisionTestingOnlyMode: true

    anchors.fill: sprite

        fixture.onBeginContact: {
            var otherBody = other.parent;
            var otherComponent = otherBody.parent;
            var collidedEntity = otherComponent.owningEntity;
            var collidedEntityType = collidedEntity.entityType;

            console.debug("Collided Enitity y: " + collidedEntity.y);
            console.debug("otherBody.height: " + otherBody.height);
            console.debug("platform.y" + platform.y);
            console.debug("sprite.height" + sprite.height);

            if(collidedEntityType === "player"){
                if(!colliding){
                    if(CollisionLogic.checkPlatformContact(collidedEntity, otherBody, platform)){
                        colliding = true;
                        collisionTestingOnlyMode = false
                        player.allowJumping(true);
                    }
                }
            }
        }

        fixture.onContactChanged: {
            var otherBody = other.parent;
            var otherComponent = otherBody.parent;
            var collidedEntity = otherComponent.owningEntity;
            var collidedEntityType = collidedEntity.entityType;


            if(collidedEntityType === "player"){
                if(!colliding){
                    if(CollisionLogic.checkPlatformContact(collidedEntity, otherBody, platform)){
                        colliding = true;
                        collisionTestingOnlyMode = false
                        player.allowJumping(true);

                    }
                }
            }
        }

        fixture.onEndContact: {
            if(colliding){
                colliding = false;
                collisionTestingOnlyMode = true;
                player.allowJumping(false);
            }
        }

    }
}
